const app = getApp()
var module=require("../../../utils/shanghai/qujinbo.js");
var parkIconB="/images/park/park_b.png";
var parkIconR="/images/park/park_r.png";
var parkIconWidthB=20;
var parkIconHeightB=30;
var parkIconWidthR=25;
var parkIconHeightR=45;
Page({
  data: {
    markers: [],
    language:'',
    currentLat:31.240527,
    currentLng:121.434674,
    personLat:0,
    personLng:0,
    parkNum:{},
    currentNum:1,
    park:[],
  },
  onLoad:function(){
    var lang=app.globalData.lang;
    var personLat;
    var personLng;
    var that=this;
    wx.getLocation({
      success:function(e){
        personLat=e.latitude;
        personLng=e.longitude;
        var park=module[lang].park;
        var markers=[];
        for (var i = park.length - 1; i >= 0; i--) {
          park[i].distance=that.getDistance(park[i].latitude,park[i].longitude,e.latitude,e.longitude);
          markers[i]={iconPath: i==(that.data.currentNum-1)?parkIconR:parkIconB,
                      id: park[i].id,latitude: park[i].latitude,longitude: park[i].longitude,
                      width: i==(that.data.currentNum-1)?parkIconWidthR:parkIconWidthB,
                      height: i==(that.data.currentNum-1)?parkIconHeightR:parkIconHeightB};
        }
        that.setData({
          language:lang,
          markers:markers,
          parkNum:module[lang].parkNum,
          park:module[lang].park,
        });
      },
      fail:function(e){
        console.log(e);
        wx.navigateBack({delta:1});
      }
    });
  },
  goThis(e){
    console.log(e);
    const location=e.currentTarget.dataset.location;
    wx.openLocation({
      latitude:e.currentTarget.dataset.lat,
      longitude:e.currentTarget.dataset.lng,
      name:e.currentTarget.dataset.name,
      scale: 14.5,
    });
  },
  initMarker(markerId){
    var markers=this.data.markers;
    for (var i = this.data.markers.length - 1; i >= 0; i--) {
      markers[i].iconPath=markerId==markers[i].id?parkIconR:parkIconB;
      markers[i].width=markerId==markers[i].id?parkIconWidthR:parkIconWidthB;
      markers[i].height=markerId==markers[i].id?parkIconHeightR:parkIconHeightB;
    }
    this.setData({
      markers:markers,
    });
  },
  changePark(e){
    this.initMarker(e.currentTarget.dataset.num);
    this.setData({
      currentNum:e.currentTarget.dataset.num,
    });
  },
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
    this.initMarker(e.markerId);
    this.setData({
      currentNum:e.markerId,
    });
  },
  getDistance(lat1, lng1, lat2, lng2){
    const earthRadius = 6367000; //approximate radius of earth in meters 
    lat1 = (lat1 * Math.PI ) / 180;
    lng1 = (lng1 * Math.PI ) / 180;
    lat2 = (lat2 * Math.PI ) / 180;
    lng2 = (lng2 * Math.PI ) / 180;
    var calcLongitude = lng2 - lng1;
    var calcLatitude = lat2 - lat1;
    var stepOne = Math.pow(Math.sin(calcLatitude / 2), 2) + Math.cos(lat1) * Math.cos(lat2) * Math.pow(Math.sin(calcLongitude / 2), 2);
    var stepTwo = 2 * Math.asin(Math.min(1, Math.sqrt(stepOne)));
    var calculatedDistance = earthRadius * stepTwo;
    if (calculatedDistance < 100) {//100米以内
        return '<100m';
    } else if (calculatedDistance < 1000) {//1公里以内
        return Math.round(calculatedDistance) + 'm';
    } else if (calculatedDistance < 100000) {//100公里以内
        return Math.round(calculatedDistance / 1000) + 'km';
    } else {//大于100公里
        return '>100km';
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: app.globalData.shareTitle,
      path: app.globalData.sharePath,
      imageUrl: '../../../images/share_img.png',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

})